﻿using System;
using System.Collections.Generic;
using System.Transactions;
using EstimateTracker.Domain.Entities;

namespace EstimateTracker.DataAccess.Repositories
{
    public class ActivityRepository : IActivityRepository
    {
        private readonly IPersistenceManager _persistenceManager;

        public ActivityRepository(IPersistenceManager persistenceManager)
        {
            if (persistenceManager == null) throw new ArgumentNullException("persistenceManager");
            _persistenceManager = persistenceManager;
        }

        public IEnumerable<Activity> FindAll()
        {
            return _persistenceManager.BuildCriteria<Activity>().List<Activity>();
        }

        public void Save(Activity activity)
        {
            using (var scope = new TransactionScope())
            {
                _persistenceManager.Persist(activity);
                scope.Complete();
            }
        }

        public IEnumerable<Activity> FindByProjectId(int id)
        {
            return _persistenceManager.GetNamedQuery<Activity>("FindByProjectId").SetParameter("Id", id).GetResultList();
        }
    }
}